using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

using PortSet;

namespace PinChanged_Tester
{
    public partial class Form1 : Form
    {
        #region Initialization
        public enum LEDColor { RedOn, RedOff, GreenOn, GreenOff }
        
        private bool RTSState = false;
        private bool DTRState = false;
        private bool RingIndicator = false;

        public Form1()
        {
            InitializeComponent();

            pictureBoxRTS.BackgroundImage = imageList1.Images[(int)LEDColor.RedOff];
            pictureBoxDTR.BackgroundImage = imageList1.Images[(int)LEDColor.RedOff];
            pictureBoxCTS.BackgroundImage = imageList1.Images[(int)LEDColor.RedOff];
            pictureBoxDSR.BackgroundImage = imageList1.Images[(int)LEDColor.RedOff];
            pictureBoxDCD.BackgroundImage = imageList1.Images[(int)LEDColor.RedOff];
            pictureBoxRI.BackgroundImage = imageList1.Images[(int)LEDColor.RedOff];
        }
        #endregion

        #region Buttons
        private void buttonRTS_Click(object sender, EventArgs e)
        {
                RTSState = !RTSState;
                if (RTSState)
                {
                    serialPort1.RtsEnable = true;
                    pictureBoxRTS.BackgroundImage = this.imageList1.Images[(int)LEDColor.RedOn];
                }
                else
                {
                    serialPort1.RtsEnable = false;
                    this.pictureBoxRTS.BackgroundImage = this.imageList1.Images[(int)LEDColor.RedOff];
                }
        }

        private void buttonDTR_Click(object sender, EventArgs e)
        {
            DTRState = !DTRState;
            if (DTRState)
            {
                serialPort1.DtrEnable = true;
                pictureBoxDTR.BackgroundImage = this.imageList1.Images[(int)LEDColor.RedOn];
            }
            else
            {
                serialPort1.DtrEnable = false;
                this.pictureBoxDTR.BackgroundImage = this.imageList1.Images[(int)LEDColor.RedOff];
            }
        }

        private void buttonCheck_Click(object sender, EventArgs e)
        {
            showCTS_DSR_CD();
        }

        #endregion

        #region Port Settings

        private void settingsToolStripMenuItem2_Click(object sender, EventArgs e)
        {
           // Make sure the port isn't already open
            if (serialPort1.IsOpen)
            {
                MessageBox.Show("The port must be closed before changing the settings.");
                return;
            }
            else
            {
                // creat an instance of the settings form
                PortSettings settings = new PortSettings();

                if (settings.ShowDialog() == DialogResult.OK)
                {
                    if (settings.selectedPort != "")
                    {
                        // set the serial port to the new settings
                        serialPort1.PortName = settings.selectedPort;
                        serialPort1.BaudRate = settings.selectedBaudrate;
                        serialPort1.DataBits = settings.selectedDataBits;
                        serialPort1.Parity = settings.selectedParity;
                        serialPort1.StopBits = settings.selectedStopBits;

                        // show the new settings in the form text line
                        showSettings();
                    }
                    else
                    {
                        MessageBox.Show("Error: Settings form returned with no COM port selected.");
                        return; // bail out
                    }
                }
                else
                {
                    MessageBox.Show("Error: buttonSetup_Click - Settings dialog box did not return Okay.");
                    return; // bail out
                }

                // open the port
                try
                {
                    serialPort1.Close();
                    serialPort1.Open();
                    menuStrip1.Items[1].Text = "Close Port";

                    showSettings();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Error - setupToolStripMenuItem_Click Exception: " + ex);
                }
            }
        }

        // show the settings in the form text line
        private void showSettings()
        {
            this.Text = "Smiley Micros - " +
                serialPort1.PortName + " " +
                serialPort1.BaudRate.ToString() + "," +
                serialPort1.Parity + "," +
                serialPort1.DataBits.ToString() + "," +
                serialPort1.StopBits;
            if (serialPort1.IsOpen)
            {
                this.Text += " - Port is open";
            }
            else
            {
                this.Text += " - Port is closed";
            }
        }

        private void openPortToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (serialPort1.IsOpen)
                {
                    serialPort1.Close();
                    menuStrip1.Items[1].Text = "Open Port";
                }
                else
                {
                    serialPort1.Open();
                    menuStrip1.Items[1].Text = "Close Port";
                }

                showSettings();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error - openPortToolStripMenuItem_Click Exception: " + ex);
            }
        }
        #endregion

        #region Modem lines
        // Show the modem states on the virtual LEDs
        private void serialPort1_PinChanged(object sender, System.IO.Ports.SerialPinChangedEventArgs e)
        {
            // Toggle RI since we can't determine the state with the SerialPort class
            if (e.EventType == SerialPinChange.Ring) RingIndicator = !RingIndicator;

            showCTS_DSR_CD();
        }

        private void showCTS_DSR_CD()
        {
            if (serialPort1.IsOpen)
            {
                if (serialPort1.CtsHolding) this.pictureBoxCTS.BackgroundImage = this.imageList1.Images[(int)LEDColor.RedOff];
                else this.pictureBoxCTS.BackgroundImage = this.imageList1.Images[(int)LEDColor.RedOn];

                if (serialPort1.DsrHolding) this.pictureBoxDSR.BackgroundImage = this.imageList1.Images[(int)LEDColor.RedOff];
                else this.pictureBoxDSR.BackgroundImage = this.imageList1.Images[(int)LEDColor.RedOn];

                if (serialPort1.CDHolding) this.pictureBoxDCD.BackgroundImage = this.imageList1.Images[(int)LEDColor.RedOff];
                else this.pictureBoxDCD.BackgroundImage = this.imageList1.Images[(int)LEDColor.RedOn];

                if (RingIndicator) this.pictureBoxRI.BackgroundImage = this.imageList1.Images[(int)LEDColor.RedOff];
                else this.pictureBoxRI.BackgroundImage = this.imageList1.Images[(int)LEDColor.RedOn];
            }
            else MessageBox.Show("Error in showCTS_DSR_CD - serailPort1 not open.");
        }


        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {

        }


    }
}

